'use strict';

Object.defineProperty(exports, "__esModule", {
    value: true
});

var _toConsumableArray2 = require('babel-runtime/helpers/toConsumableArray');

var _toConsumableArray3 = _interopRequireDefault(_toConsumableArray2);

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _inherits2 = require('babel-runtime/helpers/inherits');

var _inherits3 = _interopRequireDefault(_inherits2);

var _react = require('react');

var React = _interopRequireWildcard(_react);

var _antd = require('antd');

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj['default'] = obj; return newObj; } }

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }

/**
 * 数据字典获取纯文本
 * @author: ljq
 * @since: 2018-10-26 11:55:09
 */
var Option = _antd.Select.Option;

var DictText = function (_React$Component) {
    (0, _inherits3['default'])(DictText, _React$Component);

    function DictText(props) {
        (0, _classCallCheck3['default'])(this, DictText);

        var _this = (0, _possibleConstructorReturn3['default'])(this, (DictText.__proto__ || Object.getPrototypeOf(DictText)).call(this, props));

        _this.getText = function () {
            var _this$props = _this.props,
                type = _this$props.type,
                defaultValue = _this$props.defaultValue,
                style = _this$props.style,
                splitor = _this$props.splitor,
                dictkey = _this$props.dictkey,
                order = _this$props.order,
                storagetype = _this$props.storagetype;

            var items = void 0;
            if (storagetype && storagetype === "localStorage") {
                items = localStorage['dict_' + dictkey];
            } else {
                items = sessionStorage['dict_' + dictkey];
            }
            if (!items) {
                return '';
            }
            items = JSON.parse(items);
            if (order) {
                var orderItems = order.map(function (itemKey) {
                    return items.filter(function (item) {
                        return item.itemKey === itemKey;
                    })[0];
                });
                orderItems.push.apply(orderItems, (0, _toConsumableArray3['default'])(items.filter(function (item) {
                    return !order.includes(item.itemKey);
                })));
                items = orderItems;
            }
            if (!defaultValue) return '';
            var names = [];
            if (defaultValue instanceof Array) {
                for (var i = 0; i < items.length; i += 1) {
                    var item = items[i];
                    if (defaultValue.includes(item.itemKey)) {
                        names.push(item.itemName);
                    }
                }
            } else {
                for (var _i = 0; _i < items.length; _i += 1) {
                    var _item = items[_i];
                    if (_item.itemKey === defaultValue) {
                        names.push(_item.itemName);
                    }
                }
            }
            return names.join('、');
        };
        // 校验
        var dictkey = _this.props.dictkey;

        if (!dictkey) {
            throw new Error('Dict.Text组件需要属性：dictkey');
        }
        return _this;
    }

    (0, _createClass3['default'])(DictText, [{
        key: 'render',
        value: function render() {
            return this.getText();
        }
    }]);
    return DictText;
}(React.Component);

exports['default'] = DictText;
module.exports = exports['default'];